package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.system.domain.DtFinalResult;
import com.ruoyi.project.system.domain.DtQuery;
import com.ruoyi.project.system.domain.vo.IndexStatistic;
import com.ruoyi.project.system.domain.vo.PriceItem;
import com.ruoyi.project.system.mapper.DtFinalResultMapper;
import com.ruoyi.project.system.mapper.DtProductMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.IDtFinalResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 查询Service业务层处理
 */
@Service
public class DtFinalResultServiceImpl implements IDtFinalResultService {

    @Autowired
    private DtFinalResultMapper dtFinalResultMapper;

    @Autowired
    private DtProductMapper dtProductMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public Map<String, Object> statistics(Long userId) {
        List<IndexStatistic> indexStatistics = dtFinalResultMapper.indexStatistic(userId);
        Map ar = new HashMap<String, Object>();
        ar.put("name", indexStatistics.stream().map(IndexStatistic::getName).collect(Collectors.toList()));
        ar.put("countQuery", indexStatistics.stream().map(IndexStatistic::getCountQuery).collect(Collectors.toList()));
        ar.put("sumCostNum", indexStatistics.stream().map(IndexStatistic::getSumCostNum).collect(Collectors.toList()));
        ar.put("productId", indexStatistics.stream().map(IndexStatistic::getId).collect(Collectors.toList()));
        return ar;
    }

    @Override
    @Transactional
    public void feeDeductions(DtQuery dql) {
        BigDecimal balance = sysUserMapper.queryBalanceByUserId(dql.getUserId(), true);

        BigDecimal totalFee = new BigDecimal(0);
        List<PriceItem> priceItems = dtProductMapper.queryProductPriceList(dql.getUserId(), dql.getItems());
        List<DtFinalResult> dtFinalResults = new ArrayList<>();
        for (PriceItem priceItem : priceItems) {
            dtFinalResults.add(new DtFinalResult(dql, priceItem, balance));

            balance = balance.subtract(priceItem.getPrice());
            totalFee = totalFee.add(priceItem.getPrice());
        }
        if (totalFee.compareTo(balance) > 0) {
            throw new ServiceException("余额不足");
        } else {
            dtFinalResultMapper.insertFees(dtFinalResults);
            sysUserMapper.subtractBalance(dql.getUserId(), totalFee);
        }
    }

    @Override
    public DtFinalResult feeDeduction(Long userId, Integer productId, Integer queryId) {
        BigDecimal balance = sysUserMapper.queryBalanceByUserId(userId, true);
        PriceItem priceItem = dtProductMapper.queryProductPrice(userId, productId);
        if (priceItem.getPrice().compareTo(balance) > 0) {
            throw new ServiceException("余额不足");
        } else {
            DtFinalResult dtFinalResult = new DtFinalResult(userId, queryId, productId, balance, priceItem.getPrice());
            dtFinalResultMapper.insertFee(dtFinalResult);
            sysUserMapper.subtractBalance(userId, priceItem.getPrice());
            return dtFinalResult;
        }
    }

    /**
     * 查询查询
     *
     * @param id 查询主键
     * @return 查询
     */
    @Override
    public DtFinalResult selectDtFinalResultById(Integer id) {
        return dtFinalResultMapper.selectDtFinalResultById(id);
    }

    /**
     * 查询查询列表
     *
     * @param dtFinalResult 查询
     * @return 查询
     */
    @Override
    public List<DtFinalResult> selectDtFinalResultList(DtFinalResult dtFinalResult) {
        return dtFinalResultMapper.selectDtFinalResultList(dtFinalResult);
    }

    /**
     * 修改查询
     *
     * @param finalResultInfo 查询
     * @return 结果
     */
    @Override
    public int updateDtFinalResultInfo(DtFinalResult finalResultInfo) {
        finalResultInfo.setUpdateTime(DateUtils.getNowDate());
        return dtFinalResultMapper.updateDtFinalResultInfo(finalResultInfo);
    }

    /**
     * 批量删除查询
     *
     * @param ids 需要删除的查询主键
     * @return 结果
     */
    @Override
    public int deleteDtFinalResultByIds(Integer[] ids) {
        return dtFinalResultMapper.deleteDtFinalResultByIds(ids);
    }

}
